﻿/* Copyright 2008-10 Jarrett Vance
* see http://www.codeplex.com/blogsvc/license */
namespace AtomSite.WebCore
{
  using System.Collections.Generic;
  using System.IO;
  using System.Web.Routing;
  using System.Xml.Linq;
  using AtomSite.Domain;
  using AtomSite.Repository;
  using StructureMap;

  public interface IThemeService
  {
    IEnumerable<string> GetInstalledThemes();
    string GetThemePath(string themeName);
    Theme GetTheme(string themeName);
    string GetThemeName(Scope scope);
    string GetInheritedThemeName(Scope scope);
  }

  public class ThemeService : IThemeService
  {
    protected IAppServiceRepository AppServiceRepository { get; private set; }
    protected ILogService LogService { get; private set; }
    protected string themePath { get; private set; }

    public ThemeService(IAppServiceRepository svcRepo, string appPath, ILogService logger)
    {
      AppServiceRepository = svcRepo;
      LogService = logger;
      themePath = Path.Combine(appPath, "themes");
    }

    public IEnumerable<string> GetInstalledThemes()
    {
      foreach (string folder in Directory.GetDirectories(themePath))
      {
        if (File.Exists(Path.Combine(folder, Path.GetFileName(folder) + ".xml")))
          yield return Path.GetFileName(folder);
      }
      yield break;
    }

    public string GetThemePath(string themeName)
    {
      string path = Path.Combine(Path.Combine(themePath, themeName), themeName + ".xml");
      return path;
    }

    public Theme GetTheme(string themeName)
    {
      string path = GetThemePath(themeName);
      Theme theme = new Theme() { Xml = XElement.Load(path) };
      return theme;
    }

    /// <summary>
    /// Gets the theme name for the given scope.  Does not return inherited value.
    /// </summary>
    public string GetThemeName(Scope scope)
    {
      AppService svc = AppServiceRepository.GetService();
      if (scope.IsCollection) return svc.GetCollection(scope.Workspace, scope.Collection).Theme;
      else if (scope.IsWorkspace) return svc.GetWorkspace(scope.Workspace).Theme;
      else return svc.Theme;
    }

    /// <summary>
    /// Gets the theme name by using fallback algrorithm of inheritance.
    /// </summary>
    /// <param name="scope"></param>
    /// <returns></returns>
    public string GetInheritedThemeName(Scope scope)
    {
      AppService svc = AppServiceRepository.GetService();
      string theme = null;
      if (scope.IsCollection) theme = svc.GetCollection(scope.Workspace, scope.Collection).Theme;
      if (theme == null && (scope.IsCollection || scope.IsWorkspace)) theme = svc.GetWorkspace(scope.Workspace).Theme;
      if (theme == null) theme = svc.Theme;
      if (theme == null) theme = "default";
      return theme;
    }

    public static IThemeService GetCurrent(RequestContext ctx)
    {
      return ((IContainer)ctx.HttpContext.Application["Container"]).GetInstance<IThemeService>();
    }
  }
}
